package com.push.common.utils;

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class WebDriverUtils {
    public WebDriver driver;
    public WebDriverWait driverWait;
    private int WAIT_ELEMENT_TO_LOAD=10;

    public WebDriverUtils(WebDriver driver) {
        this.driver = driver;
    }

    public boolean isWebElementExist(By selector) {
        try {
            driver.findElement(selector);
            return true;
        } catch(NoSuchElementException e) {
            return false;
        }
    }

    public String getWebText(By by) {
        try {
            return driver.findElement(by).getText();
        } catch (NoSuchElementException e) {
            return "Text not existed!";
        }
    }

    public void clickElementContainingText(By by, String text){
        List<WebElement> elementList = driver.findElements(by);
        for(WebElement e:elementList){
            if(e.getText().contains(text)){
                e.click();
                break;
            }
        }
    }

    public String getLinkUrlContainingText(By by, String text){
        List<WebElement>subscribeButton = driver.findElements(by);
        String url = null;
        for(WebElement e:subscribeButton){
            if(e.getText().contains(text)){
                url =e.getAttribute("href");
                break;
            }
        }
        return url;
    }

    public void click(By by){
        driver.findElement(by).click();
        driver.manage().timeouts().implicitlyWait(WAIT_ELEMENT_TO_LOAD, TimeUnit.SECONDS);
    }

    public String getLinkUrl(By by){
        return driver.findElement(by).getAttribute("href");
    }

    public void sendKeys(By by, String value){
        driver.findElement(by).sendKeys(value);
    }

    public WebElement findElementByChar(String by, String value) {
        WebElement we = null;
        switch (by.toLowerCase()) {
            case "id":
            case "i":
                we = driver.findElement(By.id(value));
                break;
            case "css_selector":
            case "css":
            case "cssselector":
            case "s":
                we = driver.findElement(By.cssSelector(value));
                break;
            case "xpath":
            case "x":
                we = driver.findElement(By.xpath(value));
                break;
            case "link_text":
            case "link":
            case "text":
            case "linktext":
            case "l":
                we = driver.findElement(By.linkText(value));
                break;
            //TODO: other by type
        }
        return we;
    }

    /**
     * 最大化
     */
    public void maxBrowser() {
        driver.manage().window().maximize();
    }

    /**
     * 滚动到底部
     */
    public void scrollToBottom() {
        JavascriptExecutor driver_js = (JavascriptExecutor) driver;
        driver_js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
    }
}
